﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccessObject.DAO
{
    public class GiaoVienDAO
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="magiaovien"></param>
        /// <returns></returns>
        public List<GIAOVIEN> getByMaGiaoVien(string magiaovien)
        {
            using (QLHVEntities entities = new QLHVEntities())
            {
                var giaoviens = from gv
                              in entities.USERs.OfType<GIAOVIEN>()
                                where gv.MaGiaoVien == magiaovien
                                select gv;
                return giaoviens.ToList();      
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="giaovien"></param>
        public void insertGiaoVien(GIAOVIEN giaovien)
        {
            using (QLHVEntities entity = new QLHVEntities())
            {
                entity.USERs.AddObject(giaovien);
                entity.SaveChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<GIAOVIEN> getAllGiaoVien()
        {
            using (QLHVEntities entity = new QLHVEntities())
            {
                var giaoviens = from data in entity.USERs.OfType<GIAOVIEN>() select data;
                return giaoviens.ToList();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public List<GIAOVIEN> getGiaoVienByCMND(string p)
        {
            using (QLHVEntities entity = new QLHVEntities())
            {
                var giaoviens = from data in entity.USERs.OfType<GIAOVIEN>() 
                                where data.CMND == p
                                select data;
                return giaoviens.ToList();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="giaovien"></param>
        public void updateGiaoVien(GIAOVIEN giaovien)
        {
            using (QLHVEntities entity = new QLHVEntities())
            {
                var giaoviens = from data in entity.USERs.OfType<GIAOVIEN>()
                                where data.UserId == giaovien.UserId
                                select data;
                try
                {
                    var _giaovien = giaoviens.First();
                    _giaovien.copyData(giaovien);
                    entity.SaveChanges();
                }
                catch
                {

                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <returns></returns>
        public List<GIAOVIEN> getByMaGiaoVienById(string p1, int p2)
        {
            using (QLHVEntities entity = new QLHVEntities())
            {
                var giaoviens = from data in entity.USERs.OfType<GIAOVIEN>()
                                where data.MaGiaoVien == p1 && data.UserId != p2
                                select data;
                return giaoviens.ToList();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <returns></returns>
        public List<GIAOVIEN> getGiaoVienByCMND(string p1, int p2)
        {
            using (QLHVEntities entity = new QLHVEntities())
            {
                var giaoviens = from data in entity.USERs.OfType<GIAOVIEN>()
                                where data.CMND == p1 && data.UserId != p2
                                select data;
                return giaoviens.ToList();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="giaovien"></param>
        public void deleteGiaoVien(GIAOVIEN giaovien)
        {
            using (QLHVEntities entity = new QLHVEntities())
            {
                var gv = entity.USERs.OfType<GIAOVIEN>().FirstOrDefault(p => p.UserId == giaovien.UserId);
                entity.USERs.DeleteObject(gv);
                entity.SaveChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="giaovien"></param>
        /// <param name="khoilop"></param>
        /// <param name="monhoc"></param>
        public void updateGiaoVienChuyenMon(GIAOVIEN giaovien, KHOILOP khoilop, MONHOC monhoc)
        {
            using (QLHVEntities entity = new QLHVEntities())
            {
                var _gv = (from data in entity.USERs.OfType<GIAOVIEN>()
                           where data.UserId == giaovien.UserId
                           select data).FirstOrDefault();
                var chitiet = entity.CHITIETMONHOCs.FirstOrDefault(
                    p => p.KhoiLopId == khoilop.KhoiLopId && p.MonHocId == monhoc.MonHocId);
                _gv.CHITIETMONHOCs.Add(chitiet);
                try
                {
                    entity.SaveChanges();
                }
                catch { }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="giaovien"></param>
        /// <returns></returns>
        public List<CHITIETMONHOC> getChuyenMonByGiaoVien(GIAOVIEN giaovien)
        {
            using (QLHVEntities entity = new QLHVEntities())
            {
                var query = from data in entity.USERs.OfType<GIAOVIEN>()
                            where data.UserId == giaovien.UserId
                            select data;
                try
                {
                    var list = query.FirstOrDefault().CHITIETMONHOCs.ToList();
                    foreach (var obj in list)
                    {
                        if (obj.MONHOC != null)
                            if (obj.KHOILOP != null)
                            {

                            }
                    }
                    return list;
                }
                catch { }
                return new List<CHITIETMONHOC>();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="chitietmonhoc"></param>
        public void deleteGiaoVienChuyenMon(GIAOVIEN giaovien, CHITIETMONHOC chitietmonhoc)
        {
            using (QLHVEntities entity = new QLHVEntities())
            {
                var chitiet = entity.CHITIETMONHOCs.FirstOrDefault(p => p.ChiTietMonHocId == chitietmonhoc.ChiTietMonHocId);
                try
                {
                    entity.USERs.OfType<GIAOVIEN>().FirstOrDefault(p => p.UserId == giaovien.UserId).CHITIETMONHOCs.Remove(chitiet);
                    entity.SaveChanges();
                }
                catch { }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cHITIETMONHOC"></param>
        /// <returns></returns>
        public List<GIAOVIEN> getGiaoVienByChuyenMon(CHITIETMONHOC cHITIETMONHOC)
        {
            using (QLHVEntities entities = new QLHVEntities())
            {
                var chitiet = entities.CHITIETMONHOCs.FirstOrDefault(p => p.ChiTietMonHocId == cHITIETMONHOC.ChiTietMonHocId);
                return chitiet.USERs_GIAOVIEN.ToList();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="namhoc"></param>
        /// <returns></returns>
        public List<GIAOVIEN> getAllNoneChuNhiem(NAMHOC namhoc)
        {
            using (QLHVEntities entities = new QLHVEntities())
            {
                var chucvu = entities.LOAICHUCVUS.FirstOrDefault(p=>p.MaChucVu=="GVCN");
                var listGiaoVien = (from data in entities.CHUCVUS
                                 where data.NamHocId == namhoc.NamHocId && data.LoaiChucVuId == chucvu.LoaiChucVuId
                                 select data.UserId).ToList();
                var query = from data in entities.USERs.OfType<GIAOVIEN>()
                            where !listGiaoVien.Contains(data.UserId)
                            select data;
                return query.ToList();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public GIAOVIEN getGiaoVienById(int p)
        {
            using (QLHVEntities entities = new QLHVEntities())
            {
                return entities.USERs.OfType<GIAOVIEN>().FirstOrDefault(pp => pp.UserId == p);
            }
        }
    }
}
